/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Processo;
import util.jpaUtil;

/**
 *
 * @author calandreli
 */
public class ProcessoDAO extends JPAHibernateDAO {

    @Override
    public Class obterClasse() {
        return Processo.class;
    }
  
    public List<Processo> pesquisarProcessoAssunto(String assunto) {
        EntityManager em = jpaUtil.getEntityManager();
        //Query query = em.createQuery("Select p from Processo p INNER JOIN Assunto a ON p.assunto = a.id_assunto where LOWER(a.assunto) like :Assuntos");
        Query query = em.createQuery("select p from Processo p where p.assunto in (Select a.id_Assunto from Assunto a where LOWER(a.assunto) like :Assuntos)");
        query.setParameter("Assuntos", "" + assunto.toLowerCase() + "%");
        return query.getResultList();
    }

    public List<Processo> pesquisaProcesso(int cod) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where p.id_Processo =:codProc");
        query.setParameter("codProc", cod);
        return query.getResultList();
    }

    public Processo pesquisaProcessoSingle(int cod) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where p.id_Processo =:codProc");
        query.setParameter("codProc", cod);
        return (Processo) query.getSingleResult();
    }
     public Processo pesquisaProcessoSingleNumero(String cod) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where p.numeroProcesso =:codProc");
        query.setParameter("codProc", cod);
        return (Processo) query.getSingleResult();
    }

    public List<Processo> pesquisaProcessoData(Calendar data) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where p.data =:codProc");
        query.setParameter("codProc", data);
        return query.getResultList();
    }

    public List<Processo> pesquisaProcessoMESANO(String mes, int ano) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where p.ano =:codano and p.mes =:codmes");
        query.setParameter("codmes", mes);
        query.setParameter("codano", ano);

        return query.getResultList();
    }

    public List<Processo> pesquisaProcessoInterrese(String interrese) {
        EntityManager em = jpaUtil.getEntityManager();
        Query query = em.createQuery("Select p from Processo p where UPPER(p.interessado) like:Interresados OR LOWER(p.interessado)like :Interresados");
        query.setParameter("Interresados", "" + interrese.toLowerCase() + "%");
        return query.getResultList();
    }

}
